import java.util.*;

public class 汇总区间 {
    //https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150
    //模拟 + 双指针
    public List<String> summaryRanges(int[] nums) {
        List<String> ret = new ArrayList<>();
        int n = nums.length;
        //判空
        if(n == 0){
            return ret;
        }
        int left = 0;
        int right = 0;
        //双指针遍历
        while(right < n - 1){
            //连续即继续
            if(nums[right] + 1 == nums[right + 1]){
                right++;
            }else{
                //不连续判断长度
                if(right - left > 0){
                    StringBuilder sb = new StringBuilder();
                    sb.append(nums[left]);
                    sb.append("->");
                    sb.append(nums[right]);
                    ret.add(sb.toString());
                }else{
                    StringBuilder sb = new StringBuilder();
                    sb.append(nums[left]);
                    ret.add(sb.toString());
                }
                right++;
                left = right;
            }
        }
        //最后一个元素会跳过, 加上
        if(right - left > 0){
            StringBuilder sb = new StringBuilder();
            sb.append(nums[left]);
            sb.append("->");
            sb.append(nums[right]);
            ret.add(sb.toString());
        }else{
            StringBuilder sb = new StringBuilder();
            sb.append(nums[left]);
            ret.add(sb.toString());
        }
        return ret;
    }
}
